package com.sls.game.parser.xingqitian;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.sls.game.pojo.xingqitian.XingTradeResMsgInfo;

/**
 * 
 * @description 解析下单交易返回消息
 * @author Wangxuegang
 * @date 2012-12-26
 */
public class ParseTradeResMsg {

	private static Logger log = Logger.getLogger(ParseTradeResMsg.class);

	/**
	 * 
	 * @param xmlStr
	 * @return
	 */
	public static XingTradeResMsgInfo parseMsgOfTrade(String xmlStr) {
		XingTradeResMsgInfo info = new XingTradeResMsgInfo();
		InputSource source = null;
		StringReader read = null;
		read = new StringReader(xmlStr);
		source = new InputSource(read);

		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(source);
			Element element = document.getDocumentElement();
			NodeList itemNodes = element.getElementsByTagName("item");

			Element itemElement0 = (Element) itemNodes.item(0);
			if (itemElement0 != null) {
				String resultno = itemElement0.getAttribute("name");
				info.setResultno(resultno);
				String resultnoValue = itemElement0.getAttribute("value");
				info.setResultnoValue(resultnoValue);
			}

			Element itemElement1 = (Element) itemNodes.item(1);
			if (itemElement1 != null) {
				String retmsg = itemElement1.getAttribute("name");
				info.setRetmsg(retmsg);
				String retmsgValue = itemElement1.getAttribute("value");
				info.setRetmsgValue(retmsgValue);
			}
			Element itemEle2 = (Element) itemNodes.item(2);
			if (itemEle2 != null) {
				String strExchange_no = itemEle2.getAttribute("name");
				info.setStrExchange_no(strExchange_no);
				String strExchange_noValue = itemEle2.getAttribute("value");
				info.setStrExchange_noValue(strExchange_noValue);
			}

		} catch (ParserConfigurationException ep) {
			log.error(ep);
		} catch (SAXException es) {
			log.error(es);
		} catch (IOException ei) {
			log.error(ei);
		} catch (Exception e) {
			log.error(e);
		}

		return info;
	}

	// public void fun(){
	// StringBuilder strBui = new StringBuilder();
	// strBui.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
	// strBui.append("<items>");
	//
	// strBui.append("<item name=\"resultno\" value=\"0000\" />");
	// strBui.append("<item name=\"retmsg\" value=\"订单提交成功\" />");
	//
	// strBui.append("</items>");
	//
	// String xmlStr = strBui.toString().trim();
	// try {
	// parseMsgOfTrade(xmlStr);
	// } catch (Exception e) {
	// }
	// }
	//
	//
	// /**
	// * @param args
	// */
	// public static void main(String[] args) {
	// ParseTradeResMsg tt = new ParseTradeResMsg();
	// tt.fun();
	// }
	//

}
